package com.biodiv.miracle.web.adapter;

import com.biodiv.miracle.web.support.annotation.Controller;
import com.biodiv.miracle.web.handler.HandlerMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author: tianquan
 * @CreateTime: 2025-05-26  17:50
 * @Description: RequestMapping处理器适配器
 */
public class RequestMappingHandlerAdapter extends AbstractHandlerMethodAdapter {

    @Override
    public boolean supports(Object handler) {
        if (handler instanceof HandlerMethod handlerMethod ) {
            Controller controller = handlerMethod.getBean().getClass().getAnnotation(Controller.class);
            return controller != null;
        }
        return handler instanceof Controller;
    }

    @Override
    public Object handle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        if (handler instanceof HandlerMethod handlerMethod){
            return handlerMethod.invoke(request);
        }
        return null;
    }

}
